﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BookShop.Models
{
    public class BookShopRepository
    {
        private BookShopDataContext db = new BookShopDataContext();

        //保存
        public void Save()
        {
            db.SubmitChanges();
        }
        
        //主页显示所有图书
        public IQueryable<Book> ShowAllBook()
        {
            return db.Book;
        }
        //通过图书名得到图书
        public IQueryable<Book> GetBookByName(string bookName)
        {
            return from b in db.Book
                   where b.BookName.StartsWith(bookName)
                   select b;
        }

        //分类显示
        public IQueryable<Book> ShowBookBySort(int id)
        {
            return from b in db.Book
                   where b.SortID == id
                   select b;                  
        }


        //添加图书
        public void AddSomeBook(Book book)
        {
            db.Book.InsertOnSubmit(book); 
        }
        //删除图书
        public void DeleteBook(Book book)
        {
            db.Book.DeleteOnSubmit(book);
        }
        /// <summary>
        /// 图书详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Book GetBook(int id)
        {
            return db.Book.SingleOrDefault(b => b.BookID == id);
        }

        public IQueryable<Comment> GetComment(int id)
        {
            return from comment in db.Comment
                   where comment.BookID == id
                   select comment;
        }

        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="commentContent">内容</param>
        /// <param name="commentDate">时间</param>
        /// <param name="userName">评论人</param>
        /// <param name="bookID">评论图书ID</param>
        public void AddComment(string commentContent, DateTime commentDate, string userName, int bookID)
        {
            Comment comment = new Comment();
            comment.CommentContent = commentContent;
            comment.CommentDate = commentDate;
            comment.UserName = userName;
            comment.BookID = bookID;
            db.Comment.InsertOnSubmit(comment);
        }


        /// <summary>
        /// 购物车
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IQueryable<Cart> GetCart(string userName)
        {
            return from cart in db.Cart
                   where cart.UserName == userName
                   select cart;
        }
        /// <summary>
        /// 通过ID获取购物车信息
        /// </summary>
        /// <param name="cartID"></param>
        /// <returns></returns>
        public Cart GetAllCart(int cartID)
        {
            return db.Cart.SingleOrDefault(c => c.CartID == cartID);
        }

        /// <summary>
        /// 添加到购物车
        /// </summary>
        /// <param name="bookName">书名</param>
        /// <param name="price">价格</param>
        /// <param name="userID">用户ID</param>
        public void AddToCart(int bookId, double price, string userName,string count)
        {
            if (GetCart(userName).Count() != 0)
            {
                foreach (var c in GetCart(userName))
                {
                    if (c.Book.BookID == bookId)
                    {
                        c.Count = c.Count + Convert.ToInt32(count);
                    }
                    else
                    {
                        Cart cart = new Cart();
                        cart.BookID = bookId;
                        cart.Price = price;
                        cart.UserName = userName;
                        cart.Count = Convert.ToInt32(count);
                        db.Cart.InsertOnSubmit(cart);
                    }
                }
            }
            else
            {
                Cart cart = new Cart();
                cart.BookID = bookId;
                cart.Price = price;
                cart.UserName = userName;
                cart.Count = Convert.ToInt32(count);
                db.Cart.InsertOnSubmit(cart);
            }

            
        }

        //删除购物车信息
        public void DeleteCart(Cart cart)
        {
            db.Cart.DeleteOnSubmit(cart);
        }
        /// <summary>
        /// 显示所有图书分类
        /// </summary>
        /// <returns>所有分类</returns>
        public IQueryable<Sort> ShowAllSort()
        {
            return db.Sort;
        }

        //添加地址
        public void AddAdress(Adress adress)
        {
            db.Adress.InsertOnSubmit(adress);
        }
        //地址列表
        public IQueryable<Adress> ShowAdress(string userName)
        {
            return from adress in db.Adress
                   where adress.UserName == userName
                   select adress;
        }
        //获取地址
        public Adress GetAdress(int adressID)
        {
            return db.Adress.SingleOrDefault(a => a.AdressID == adressID);
        }

        //删除地址
        public void DeleteAdress(Adress adress)
        {
            db.Adress.DeleteOnSubmit(adress);
        }
        //购买产生订单

        public void AddOrder(Orders order)
        {
            db.Orders.InsertOnSubmit(order);
        }
        public void AddOrderItem(OrderItem orderItem)
        {
            //插入到OrderItem表
            db.OrderItem.InsertOnSubmit(orderItem);
        }
        //获得订单项

        public IQueryable<OrderItem> GetOrderItem(int id)
        {
            var orderItem = from o in db.OrderItem
                            where o.OrderID == id
                            select o;
            return orderItem;
        }
        //显示所有我的订单
        public IQueryable<Orders> ShowMyOrder(string userName)
        {
            var myOrder = from o in db.Orders
                          where o.UserName == userName
                          orderby o.Status
                          select o;
            return myOrder;
        }


        //显示所有订单
        public IQueryable<Orders> ShowAllOrder()
        {
            return from allOrder in db.Orders
                   orderby allOrder.Status
                   select allOrder;
        }
        //用户删除订单，标记IsDeleteD为true
        public void UserDeleteBookOrder(Orders order)
        {
            db.Orders.DeleteOnSubmit(order);
        }
        //等待处理的订单
        public IQueryable<Orders> WaitHandleOrder()
        {
            return from allOrder in db.Orders
                   where allOrder.Status=="处理中"
                   orderby allOrder.OrderDate
                   select allOrder;
        }


        //已经发货的订单
        public IQueryable<Orders> HandleOrder()
        {
            return from allOrder in db.Orders
                   where allOrder.Status == "发货"
                   orderby allOrder.OrderDate
                   select allOrder;
        }

        //完成的订单
        public IQueryable<Orders> FinishOrder()
        {
            return from allOrder in db.Orders
                   where allOrder.Status == "完成"
                   orderby allOrder.OrderDate
                   select allOrder;
        }

        //获取订单
        public Orders GetOrder(int id)
        {
            return db.Orders.SingleOrDefault(o => o.OrderID == id); 
        }


        
    }
}